<template>
  <d2-container>
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="queryItems[0].value" :placeholder="$t('arbitration.name')" clearable/>
      </el-form-item>
      <el-form-item>
        <el-select
                v-model="queryItems[1].value"
                filterable
                reserve-keyword
                clearable
                placeholder="请选择诉讼类型"
                style="width: 100%"  >
          <el-option
                  v-for="item in arbitrationFlowNameList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input v-model="queryItems[2].value" :placeholder="$t('arbitration.applyPersonName')" clearable/>
      </el-form-item>
      <el-form-item>
        <el-input v-model="queryItems[3].value" :placeholder="$t('arbitration.idNumber')" clearable/>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">{{ $t('query') }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="$hasPermission('sys:arbitration:save')" type="primary" @click="addOrUpdateHandle()">{{ $t('add') }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="$hasPermission('sys:arbitration:delete')" type="danger" @click="deleteHandle()">{{ $t('deleteBatch') }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="$hasPermission('sys:arbitration:export')" type="primary" @click="exportExcel()">{{ $t('export') }}</el-button>
      </el-form-item>
    </el-form>
    <el-table
      size="mini"
      v-loading="dataListLoading"
      :data="dataList"
      border
      stripe
      @selection-change="dataListSelectionChangeHandle"
      @sort-change="dataListSortChangeHandle"
      style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50"/>
      <el-table-column prop="name" :label="$t('arbitration.name')" header-align="center" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="detailHandle(scope.row)">{{ scope.row.name }}</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="arbitrationFlowName" :label="$t('arbitration.arbitrationFlowName')" header-align="center" align="center"/>
      <el-table-column prop="applyPersonName" :label="$t('arbitration.applyPersonName')" header-align="center" align="center"/>
      <el-table-column prop="idNumber" :label="$t('arbitration.idNumber')" header-align="center" align="center"  />
      <el-table-column prop="disputeMoney" :label="$t('arbitration.disputeMoney')" header-align="center" align="center"/>
      <el-table-column prop="arbitrationFlowTypeName" :label="$t('arbitration.arbitrationFlowTypeName')" header-align="center" align="center"/>
      <el-table-column prop="flowDeadline" :label="$t('arbitration.flowDeadline')" header-align="center" align="center" :formatter="dateFormat" />
      <el-table-column prop="createTime" :label="$t('createTime')" sortable="custom" header-align="center" align="center" width="180"/>
      <el-table-column prop="remark" :label="$t('remark')" header-align="center" align="center"/>
      <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
        <template slot-scope="scope">
          <el-button v-if="$hasPermission('sys:arbitration:look')" type="text" size="mini" @click="detailHandle(scope.row)">{{ $t('look') }}</el-button>
          <el-button v-if="$hasPermission('sys:arbitration:update')" type="text" size="mini" @click="addOrUpdateHandle(scope.row.id)">{{ $t('update') }}</el-button>
          <el-button v-if="$hasPermission('sys:arbitration:delete')" type="text" size="mini" @click="deleteHandle(scope.row.id)">{{ $t('delete') }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"/>
    <!-- 分页 -->
    <el-pagination
      slot="footer"
      :current-page="page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="limit"
      :total="total"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="pageSizeChangeHandle"
      @current-change="pageCurrentChangeHandle">
    </el-pagination>
  </d2-container>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './arbitration-add-or-update'
import { mapMutations } from 'vuex'
import { moduleRoutes } from '@/router'
export default {
  mixins: [ mixinViewModule ],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/arbitration/page',
        getDataListIsPage: true,
        deleteURL: '/arbitration/delete',
        deleteIsBatch: true
      },
      dataForm: {

      },
      queryItems: [
        {
          field: 'name',
          as: 'LIKE',
          value: ''
        },
        {
          field: 'arbitrationFlowId',
          as: 'EQ',
          value: ''
        },
        {
          field: 'applyPersonName',
          as: 'LIKE',
          value: ''
        },
        {
          field: 'idNumber',
          as: 'LIKE',
          value: ''
        }
      ],
      arbitrationFlowNameList:[],
      order: 'DESC',
      orderField: 'createTime',
      columns: [
        {
          label: 'ID',
          prop: 'id'
        },
        {
          label: '诉讼名称',
          prop: 'name'
        },
        {
          label:'申请人姓名',
          prop: 'applyPersonName'
        },
        {
          label: '身份证号码',
          prop: 'idNumber'
        },
        {
          label: '争议金额(元)',
          prop: 'disputeMoney'
        },
        {
          label: '诉讼流程名称',
          prop: 'arbitrationFlowTypeName'
        },
        {
          label: '当前流程截止日期',
          prop: 'flowDeadline'
        },
        {
          label: '创建时间',
          prop: 'createTime'
        },
        {
          label: '备注',
          prop: 'remark'
        }
      ]
    }
  },
  components: {
    AddOrUpdate
  },
  methods: {
    ...mapMutations('d2admin/page', {
      d2adminPagePoolPush: 'push'
    }),

    dateFormat:function(row,column) {
      const dateStr = row[column.property]
      if (dateStr != null) {
        const dateMat = new Date(dateStr);
        const year = dateMat.getFullYear();
        const month = dateMat.getMonth() + 1;
        const day = dateMat.getDate();
        const hh = dateMat.getHours();
        const mm = dateMat.getMinutes();
        const ss = dateMat.getSeconds();
        // const timeFormat = year + "-" + month + "-" + day + " " + hh + ":" + mm + ":" + ss;
        // const timeFormat = year + "-" + month + "-" + day;
        let timeFormat = year+'-'+ (month<10?'0'+month:month)+'-'+ (day<10?'0'+day:day);
        return timeFormat;
      }
    },

    // 获取诉讼类型列表
    getLawsuitFlowIdList:function() {
      return this.$axios.get('/mananger/arbitrationFlow/page', {
        params: {
          order: 'ASC',
          orderField: 'createTime',
          current: 1,
          size: 20,
          listMode: true,
          queryItems: 'parentId,EQ,0'
        }
      }).then(res => {
        this.arbitrationFlowNameList = res.records
      }).catch(() => {
      })
    },

    detailHandle:function (row) {
      // 组装路由名称, 并判断是否已添加, 如是: 则直接跳转
      var routeName = `arbitration-arbitrationDatum`
      var route = window.SITE_CONFIG['dynamicRoutes'].filter(item => item.name === routeName)[0];
      if (route) {
        return this.$router.push({ name: routeName, params: { 'parentId': row.id , 'name': row.name} })
      }
      // 否则: 添加并全局变量保存, 再跳转
      route = {
        path: routeName,
        component: () => import(`@/views/zfw/arbitration/arbitrationDatum`),
        name: routeName,
        meta: {
          ...window.SITE_CONFIG['contentTabDefault'],
          menuId: this.$route.meta.menuId,
          title: `诉讼详情`
        }
      };
      this.$router.addRoutes([
        {
          ...moduleRoutes,
          name: `main-dynamic__${route.name}`,
          children: [route]
        }
      ]);
      window.SITE_CONFIG['dynamicRoutes'].push(route);
      this.d2adminPagePoolPush(route);
      this.$router.push({ name: route.name, params: { 'parentId': row.id, 'name': row.name } })
    },
    exportExcel() {
      this.$export.excel({
        columns: this.columns,
        data: this.dataList,
        header: '诉讼案件统计',
        merges: ['A1', 'I1']
      }).then(() => {
        this.$message({
          message: '导出表格成功',
          type: 'success'
        });
      })
    }

  },
  mounted:function() {
    this.getLawsuitFlowIdList();
  }
}
</script>
